<?php

/**
 * This is the model class for table "{{bookable_resource}}".
 *
 * The followings are the available columns in table '{{bookable_resource}}':
 * @property integer $_id
 * @property integer $timeslot_id
 * @property integer $resource_id
 * @property integer $space_id
 * @property integer $app_id
 * @property integer $creator_id
 * @property integer $resource_amount
 * @property string $unit_price
 * @property string $price_expression
 * @property string $booking_open_time
 * @property string $booking_end_time
 * @property string $repeat_start_time
 * @property string $repeat_end_time
 * @property integer $priority
 * @property string $image_urls
 * @property integer $status
 */
class BSMBookableResource extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{bookable_resource}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('timeslot_id, resource_id, space_id, app_id, creator_id, resource_amount, priority, status', 'numerical', 'integerOnly'=>true),
			array('unit_price', 'length', 'max'=>10),
			array('price_expression', 'length', 'max'=>100),
			array('booking_open_time, booking_end_time, repeat_start_time, repeat_end_time', 'length', 'max'=>45),
			array('image_urls', 'length', 'max'=>1024),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('_id, timeslot_id, resource_id, space_id, app_id, creator_id, resource_amount, unit_price, price_expression, booking_open_time, booking_end_time, repeat_start_time, repeat_end_time, priority, image_urls, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'timeslot'=>array(self::BELONGS_TO, 'BSMTimeslot', 'timeslot_id'),
            'resource'=>array(self::BELONGS_TO, 'BSMResource', 'resource_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'_id' => 'ID',
			'timeslot_id' => 'Timeslot',
			'resource_id' => 'Resource',
			'space_id' => 'Space',
			'app_id' => 'App',
			'creator_id' => 'Creator',
			'resource_amount' => 'Resource Amount',
			'unit_price' => 'Unit Price',
			'price_expression' => 'Price Expression',
			'booking_open_time' => 'Booking Open Time',
			'booking_end_time' => 'Booking End Time',
			'repeat_start_time' => 'Repeat Start Time',
			'repeat_end_time' => 'Repeat End Time',
			'priority' => 'Priority',
			'image_urls' => 'Image Urls',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('_id',$this->_id);
		$criteria->compare('timeslot_id',$this->timeslot_id);
		$criteria->compare('resource_id',$this->resource_id);
		$criteria->compare('space_id',$this->space_id);
		$criteria->compare('app_id',$this->app_id);
		$criteria->compare('creator_id',$this->creator_id);
		$criteria->compare('resource_amount',$this->resource_amount);
		$criteria->compare('unit_price',$this->unit_price,true);
		$criteria->compare('price_expression',$this->price_expression,true);
		$criteria->compare('booking_open_time',$this->booking_open_time,true);
		$criteria->compare('booking_end_time',$this->booking_end_time,true);
		$criteria->compare('repeat_start_time',$this->repeat_start_time,true);
		$criteria->compare('repeat_end_time',$this->repeat_end_time,true);
		$criteria->compare('priority',$this->priority);
		$criteria->compare('image_urls',$this->image_urls,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * @return CDbConnection the database connection used for this class
	 */
	public function getDbConnection()
	{
		return Yii::app()->bookingdb;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return BSMBookableResource the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    
    public function getList($resources)
    {
        $ids = array();
        foreach ($resources as $resource) {
            $ids[] = $resource->_id;
        }
        
        $criteria = new CDbCriteria;
        $criteria->with = array(
            'timeslot',
        );
        $criteria->addInCondition("t.resource_id", $ids);
        $criteria->together = true;
        
        $list = self::model()->findAll($criteria);
        
        return $list;
    }
    
    public function calcPrice($amount = 1)
    {
        return $this->unit_price * $amount;
    }
}
